package com.xxx.demo.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xxx.demo.dto.ShowAllDto;
import com.xxx.demo.entity.ShowType;
import com.xxx.demo.entity.TUsers;
import com.xxx.demo.service.ShowTypeService;
import com.xxx.demo.vo.ShowAllVo;
import com.xxx.demo.vo.ShowTypeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
public class ShowTypeController {
    @Autowired
    private ShowTypeService showTypeService;

    /**
     * 展示类型分页查询
     * @param showType
     * @param pageNo
     * @param pageSize
     * @param httpSession
     * @param model
     * @return
     */
    @RequestMapping("/findShowTypePage")
    public String findShowTypePage(@ModelAttribute("showType") ShowType showType, Integer pageNo, Integer pageSize, HttpSession httpSession, Model model){
        if (pageNo==null || pageNo<1){
            pageNo=1;
        }
        if (pageSize==null || pageSize<1){
            pageSize=3;
        }
        IPage<ShowTypeVo> showTypeIPage=showTypeService.findShowTypePage(pageNo,pageSize,showType);
        TUsers users= (TUsers) httpSession.getAttribute("user");
        model.addAttribute("adminSession",users);
        model.addAttribute("showTypePage",showTypeIPage);
        return "showTypePage";
    }

    @RequestMapping("/insertShowType")
    @ResponseBody
    public String insertShowType(ShowType showType){
        int i = showTypeService.addShowType(showType);
        if (i == 0){
            return "0";
        }
        return "1";
    }

    /**
     * 根据id查详情
     * @param model
     * @param id
     * @return
     */
    @RequestMapping("/findShowTypeById")
    public String findShowTypeById(Model model,Long id,HttpSession session){
        ShowType showTypeById = showTypeService.findShowTypeById(id);
        model.addAttribute("showType",showTypeById);
        model.addAttribute("showTypes",showTypeService.selectAll());
        TUsers admin1= (TUsers) session.getAttribute("user");
        model.addAttribute("adminSession",admin1);
        return "updateShowType";
    }

    /**
     * 修改
     * @param showType
     * @return
     */
    @RequestMapping("/updateShowType")
    @ResponseBody
    public String updateShowType(ShowType showType){
        int i = showTypeService.updateShowType(showType);
        if (i == 0){
            return "0";
        }
        return "1";
    }

    /**
     * 根据id删除（逻辑删除）
     * @param id
     * @return
     */
    @RequestMapping("deleteShowTypeById")
    @ResponseBody
    public String deleteShowTypeById(String id){
        id = id.replace("!","");
        System.out.println(id);
        Long aLong = Long.valueOf(id);
        int i = showTypeService.deleteById(aLong);
        if (i == 0){
            return "0";
        }
        return "1";
    }
}
